package com.shanghaionstar.carmanage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shanghaionstar.R;
import com.shanghaionstar.carmanage.model.VechileReportItem;

import java.util.List;

/**
 * Created by Mengzhou Gu on 2016/7/28.
 */
public class VechileReportAdapter extends BaseExpandableListAdapter {
    private List<VechileReportItem> list;
    private Context context;

    public VechileReportAdapter(Context context, List<VechileReportItem> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        VechileReportItem vechileReportItem = list.get(groupPosition);
        int childCount = -1;
        switch (vechileReportItem.icon) {
            case 0:
                childCount = 0;
                break;
            default:
                childCount = 1;
                break;
        }
        return childCount;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        VechileReportItem vechileReportItem = list.get(groupPosition);
        String des = null;
        switch (vechileReportItem.icon) {
            case 0:
                des = "";
                break;
            default:
                des =vechileReportItem.content;
                break;
        }
        return des;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.discover_ovd_condition_group_item, null);
            holder = new ViewHolder();
            holder.titleTv = (TextView) convertView.findViewById(R.id.item_title_tv);
            holder.contentTv = (TextView) convertView.findViewById(R.id.item_content_tv);
            holder.arrowIv = (ImageView) convertView.findViewById(R.id.arrow_iv);
            holder.stateIv = (ImageView) convertView.findViewById(R.id.state_iv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        VechileReportItem item = list.get(groupPosition);
        if (holder != null) {
            int resId = getStatusDrawableId(item.icon);
            holder.stateIv.setImageDrawable(context.getResources().getDrawable(resId));
            if (item.icon == 0) {
                holder.arrowIv.setVisibility(View.GONE);
            } else {
                holder.arrowIv.setVisibility(View.VISIBLE);
                if (isExpanded) {
                    holder.arrowIv.setImageResource(R.drawable.gray_arrow_up);
                } else {
                    holder.arrowIv.setImageResource(R.drawable.gray_arrow_down);
                }
            }
            holder.titleTv.setText(item.title);
            holder.contentTv.setText(getOVDContent(item.icon));
        }
        return convertView;
    }

    private String getOVDContent(int status) {
        if (status == 0) {
            return context.getString(R.string.goodContent);
        } else if (status == 1) {
            return context.getString(R.string.adequateContent);
        } else {
            return context.getString(R.string.criticalContent);
        }
    }

    private int getStatusDrawableId(int status) {
        if (status == 0) {
            return R.drawable.v_c_c;
        } else if (status == 1) {
            return R.drawable.v_c_p;
        } else {
            return R.drawable.v_c_u;
        }
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.discover_ovd_condition_children_item,
                    null);
            holder = new ChildHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.child_tv);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        holder.tv.setText(list.get(groupPosition).content);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    class ViewHolder {
        TextView  titleTv;
        TextView  contentTv;
        ImageView arrowIv;
        ImageView stateIv;
    }
    class ChildHolder{
        TextView tv;
    }
}
